/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <QFile>
#include <QFileInfo>
#include <QSettings>
#include <QRegExp>
#include "kusabaagent.h"
#include "log.h"
#include "components/chan.h"
#include "components/board.h"
#include "tasks/tasksendpost.h"
#include "kusabatasksendpost.h"

namespace Wishmaster
{

	KusabaAgent::KusabaAgent(const QString& host) : DefaultAgent(host),
		m_host(host), m_deleteRequest(0)
	{
	}

	KusabaAgent::~KusabaAgent()
	{
	}

	void KusabaAgent::makePost(const PostDescriptor& post, const QString& captcha)
	{
		TaskPtr task = TaskPtr(new KusabaTaskSendPost(post, captcha, requestManager(), tr("Sending post"), this));
		connect(task.data(), SIGNAL(result(KusabaTaskSendPost*,QByteArray,QString)), this, SLOT(resultPost(KusabaTaskSendPost*,QByteArray,QString)));
		taskhub()->queueTask(task);
	}

	void KusabaAgent::createThread(const PostDescriptor& post, const QString& captcha)
	{
		TaskPtr task = TaskPtr(new KusabaTaskSendPost(post, captcha, requestManager(), tr("Sending post"), this));
		connect(task.data(), SIGNAL(result(KusabaTaskSendPost*,QByteArray,QString)), this, SLOT(resultThread(KusabaTaskSendPost*,QByteArray,QString)));
		taskhub()->queueTask(task);
	}

	void KusabaAgent::deleteThread(const ThreadId& threadId, const QString& password)
	{
//		QString plainboardname = threadId.boardId();
//		plainboardname.remove('/');
//
//		QHttpRequestHeader header("POST", "/board.php");
//		header.setValue("Host", m_host);
//		header.setValue("User-Agent", "Wishmaster");
//		header.setValue("Accept", "*/*");
//		header.setValue("Connection", "Keep-Alive");
//		header.setValue("Content-type", "application/x-www-form-urlencoded");
//		QString rq = QString("board=") + plainboardname + "&post%5B%5D=" + threadId.threadStringId() + "&postpassword=" + password;
//		m_deleteRequest = m_http.request(header, rq.toUtf8());
	}

	void KusabaAgent::deletePost(const PostId& postId, const QString& password)
	{
//		QString plainboardname = postId.board();
//		plainboardname.remove('/');
//
//		QString referer = QString("http://%1%2res/%3.html").arg(m_host).arg(postId.board()).arg(postId.threadId());
//
//		QHttpRequestHeader header("POST", "/board.php");
//		header.setValue("Host", m_host);
//		header.setValue("User-Agent", "Wishmaster");
//		header.setValue("Referer", referer);
//		header.setValue("Accept", "*/*");
//		header.setValue("Connection", "Keep-Alive");
//		header.setValue("Content-type", "application/x-www-form-urlencoded");
//		QString rq = QString("board=") + plainboardname + "&post%5B%5D=" + QString::number(postId.postId()) + "&postpassword=" + password + "&deletepost=" + property("delete_value").toString() + "&reportreason=";
//		m_deleteRequest = m_http.request(header, rq.toUtf8());
	}

	void KusabaAgent::resultPost(KusabaTaskSendPost* task, const QByteArray& data, const QString& contentType)
	{
		emit postingDone(task->postDescriptor().parent_thread);
	}
	
	void KusabaAgent::resultThread(KusabaTaskSendPost* task, const QByteArray& data, const QString& contentType)
	{
		emit postingDone(task->postDescriptor().parent_thread);
	}

	QStringList KusabaAgent::ratings()
	{
		return QStringList();
	}

	ChanAgent* KusabaAgent::clone(const QString& host)
	{
		return new KusabaAgent(host);
	}

	QString KusabaAgent::id()
	{
		return "kusaba";
	}
}

Q_EXPORT_PLUGIN2(kusabaagent, Wishmaster::KusabaAgent)
